package ModelLayer;
import java.util.ArrayList;
/**
 * Container for Employee objects.
 * 
 * @author (Gruppe 3)
 */
public class EmployeeContainer
{
    private ArrayList<Employee> employees;
    private static EmployeeContainer instance;
    private int index;

    /**
     * Constructor
     */
    private EmployeeContainer()
    {
        employees = new ArrayList<Employee>();
        index = 0;
    }


    /**
     * Singleton
     */
    public static EmployeeContainer getInstance()
    {
        if (instance == null)
        {
            instance = new EmployeeContainer();
        }
        return instance;
    }
    
    /**
     * Add an employee to the ArrayList employees
     * @param name          name of the employee to be added
     * @param address       address of the employee to be added
     * @param phone         phone of the employee to be added
     * @param email         email of the employee to be added
     * @param employeeID    id of the employee to be added
     */
    public void addEmployee(String name, String address, String phone, String email, String employeeID)
    {
        Employee newemployee = new Employee(name, address, phone, email, employeeID);
        employees.add(newemployee);
    }
    
    /**
     * Finds an employee in the ArrayList employees
     * @param searchID  id of the wanted employee
     * @return true, if employe was found, otherwise returns a false
     */
    public boolean findEmployee(String searchID)
    {
        index = 0;
        boolean found = false;
        while (!found && index < employees.size()){
            Employee id = employees.get(index);
            if(id.getEmployeeID().equals(searchID))
            {
                found = true;
                return found;
            }
            else{
                index++;
            }
        }
        return false;
    }  
    
    /**
     * Deletes an employee in the ArrayList employees
     */
    public void deleteEmployee(String ID)
    {
        findEmployee(ID);
        employees.remove(index);
    }
    
    /**
     * Updates an employee in the ArrayList employees
     */
    public void updateEmployee(String name, String address, String phone, String email, String employeeID, String ID)
    {
        findEmployee(ID);
        if(name != null)
        {
            employees.get(index).setName(name);
        }
        
        if(address != null)
        {
            employees.get(index).setPhone(phone);
        }
        
        if(address != null)
        {
            employees.get(index).setAddress(address);
        }
        
        if(phone != null)
        {
            employees.get(index).setEmail(email);
        }
        
        if(employeeID != null)
        {
            employees.get(index).setEmployeeID(employeeID);
        }
    }
    
    /**
     * Find and list an employee in the ArrayList employees
     * @param employeeID  id of the wanted employee
     */
    public void listEmployee(String employeeID)
    {
        findEmployee(employeeID);
        System.out.println("Name: " + employees.get(index).getName());
        System.out.println("Address: " + employees.get(index).getAddress());
        System.out.println("Phone: " + employees.get(index).getPhone());
        System.out.println("Email: " + employees.get(index).getEmail());
        System.out.println("EmployeeID: " +employees.get(index).getEmployeeID());
        System.out.println();   
    }
    
    /**
     * List all employees in the ArrayList employees
     */
    public void listAllEmployees()
    {
        for(Employee e : employees)
        {
            System.out.println("Name: " + e.getName());
            System.out.println("Address: " + e.getAddress());
            System.out.println("Phone: " + e.getPhone());
            System.out.println("Email: " + e.getEmail());
            System.out.println("EmployeeID: " + e.getEmployeeID());
            System.out.println();
        }
    }
    
    /**
     * Find and return a wanted employee in the ArrayList employees
     * @param searchID  id of the wanted person to return
     * @return found employe, otherwise null
     */
    public Employee getEmployee(String searchID)
    {
        if (findEmployee(searchID))
        {
            return employees.get(index);
        }
        
        return null;
    }
    
    public ArrayList<Employee> getEmployeesArrayList()
    {
    	return employees;
    }
}